package sto.orz.commons;

import org.hibernate.HibernateException;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateHelper  {
	private static HibernateHelper singleton = new HibernateHelper();

	/**
	 * @return the singleton instance of this class
	 */
	public static HibernateHelper get() {
		return singleton;
	}

	/**
	 * Export to the database the table schema for persistent classes.
	 */
	public void schemaExport() throws HibernateException {
		AnnotationConfiguration cfg = new AnnotationConfiguration();
//		Properties props = new Properties();
//		try {
//			props.load(this.getClass().getResourceAsStream(
//					"./src/test/resources/database.properties"));
//		} catch (FileNotFoundException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		cfg.setProperties(props);

		cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
		cfg.addFile("./src/main/java/sto/orz/model/Role.hbm.xml");
		cfg.addFile("./src/main/java/sto/orz/model/User.hbm.xml");
		cfg.addAnnotatedClass(sto.orz.rptp.model.Definition.class);
		cfg.addAnnotatedClass(sto.orz.rptp.model.Dataset.class);
		cfg.addAnnotatedClass(sto.orz.rptp.model.Datasource.class);
		cfg.addAnnotatedClass(sto.orz.rptp.model.Attribute.class);

		new org.hibernate.tool.hbm2ddl.SchemaExport(cfg).setOutputFile(
				"./rptp_db.sql").create(true, false);
	}

	public static void main(String[] args) {
		HibernateHelper h = HibernateHelper.get();
		h.schemaExport();
	}
}